package com.block.common.security.service.impl.reqData;

import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.core.codec.Base64Encoder;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.crypto.symmetric.AES;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.security.service.ReqDataTransferService;
import com.block.common.service.LogFactory;
import com.block.common.util.AESOperator;
import com.block.common.util.LogFilterUtil;
import com.block.db.entity.AppEncrypt;
import org.bouncycastle.crypto.engines.RSAEngine;
import org.slf4j.Logger;

import java.io.UnsupportedEncodingException;

public class MiniReqTransferService extends ReqDataTransferService {
    private static Logger logger= LogFactory.getAppReq();
    @Override
    public JSONObject transferReqData(AppEncrypt appEncrypt, JSONObject jsonObject, String methodName) throws Exception {
        String encTranData=jsonObject.getString("tranData");
        String ciphertext=jsonObject.getString("ciphertext");

        String randKey= rsaDecode(ciphertext, appEncrypt.getSecretKey());

        String tranData=  SecureUtil.aes(randKey.getBytes()).decryptStr(encTranData);

        JSONObject logJson= LogFilterUtil.filterLogs(tranData);

        logger.info("解密后的tranData{}",JSON.toJSONString(logJson));

        return JSONObject.parseObject(tranData);
    }


    public static String rsaDecode(String content, String priKey) throws UnsupportedEncodingException {

        RSA rsa = SecureUtil.rsa(priKey, null);
//        byte[] decrypt = rsa.decrypt(new String(Base64Decoder.decode(AESUtil.parseHexStr2Byte(content))), KeyType.PrivateKey);
        byte[] decrypt = rsa.decrypt(new String(Base64Decoder.decode(content)), KeyType.PrivateKey);
        return new String(decrypt, "UTF-8");
    }


    public static String rsaEncode(String content, String pubKey) throws UnsupportedEncodingException {

        RSA rsa = SecureUtil.rsa(null, pubKey);
        byte[] decrypt = rsa.encrypt(content, KeyType.PublicKey);
        return Base64Encoder.encode(decrypt);
    }


    /**
     * {"ciphertext":"eGxQK2RpM2kwb3QrUks1UkZyM0FKTElPeldXMFRlWjBHcjRxYlY1ZkM4RjZKKzFQbXVHMStscjVBRUdEeE9pNUlaV3Q3L0MwTnkxNGhIQ0lwUnR2dU1Ka0VGdkdpWHhxMW1NbHd1VzdFY29Hc0JyUmxyQzJGc2tTQXNtaGFaN3JaME1zSVZrbDc1d3FtV3ZldkZpTWUrRFFWVzBxalROelRIcDIxRnhKbmJvPQ==",
     * "tranData":"ekozbTlIbmpCUUFPSDdiaDVmcWpvZnZZNXFHZVZaeDdzN1hyelpnb0o1eE5PNFBrNURsMWlGSmdTTlpFWEwzYw==","appId":"testApp","version":"1.0.1"}
     * @param args
     * @throws UnsupportedEncodingException
     */
    public static void main(String[] args) throws UnsupportedEncodingException {
        String content = "nihaoa";

        String aesKey = RandomUtil.randomString(32);
        System.out.println(aesKey);

        String pubKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDQaCswD+C3DzMiZZyHIJr5g3Tc5nChFW1o22q46iR2gdt3b45o4XrIdARzTfKYeH/Dcm+K15dbqRtDslW4z09HSwt2Wzta1LAVG/amkz221Ufh60MDuKOCYEUhYelXzd8XNA3nhYdGhznLYllWC9D3hsgWHh/Pwrh/VNPe2eK29QIDAQAB";
        String priKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBANBoKzAP4LcPMyJlnIcgmvmDdNzmcKEVbWjbarjqJHaB23dvjmjhesh0BHNN8ph4f8Nyb4rXl1upG0OyVbjPT0dLC3ZbO1rUsBUb9qaTPbbVR+HrQwO4o4JgRSFh6VfN3xc0DeeFh0aHOctiWVYL0PeGyBYeH8/CuH9U097Z4rb1AgMBAAECgYB/NyctxnNwqymWlJow7hlLuA/tDAmyesb3nACQcfTiFjCmvoUaxpDRrehNjkZJBj1PTBqEkUfGXvrSnxiuoHWOQFT/SqJVMs2Oi+Yn2FTOC62DFhm0tAhmoHoxyhcRq5BPC0Sr97ZvkLS73C7AM03XRaqpw7IrzPNehoQFyubkIQJBAOv1ljR5PpZ4KzRChKr/NltqPhPpCcMEIvtNOaHvIb0kqI8ecT0vxoO59VQXojIVqon4aQ4PoVmlxg6Ws2AuYhkCQQDiG4QEoJeml0dbKPBNy5hs1v/H7+eOmeWoJFVNadV/U2URQAUiyEV5DqF0bcPV2nybbboj8ijSuq/XukdTQO89AkA35WyYr6Qg9wSdE6jj7SNOUoO0Kb9TQFadG3UspD5k1RtNFBQVP40SRWoVaqfUn4dPudYBK7i4SOn7KL4Dadl5AkEAgRoM60d2z5wZFift7b2OLrckHNkScg3VX4g/XJBNYwIO9yaCyDVp1ZFRBtWVuAmklGq2mcevH4OQ27B1AUhWUQJBAJaUyg6ESJLHta86rhSIbtqKXDw9jts+SeBpSpNVLPsbWju4FNvWD0ZST323W1h0ViKjX8l+EckIRaK0marChBU=";

        String encodeResult = rsaEncode(aesKey, pubKey);

        encodeResult = "eGxQK2RpM2kwb3QrUks1UkZyM0FKTElPeldXMFRlWjBHcjRxYlY1ZkM4RjZKKzFQbXVHMStscjVBRUdEeE9pNUlaV3Q3L0MwTnkxNGhIQ0lwUnR2dU1Ka0VGdkdpWHhxMW1NbHd1VzdFY29Hc0JyUmxyQzJGc2tTQXNtaGFaN3JaME1zSVZrbDc1d3FtV3ZldkZpTWUrRFFWVzBxalROelRIcDIxRnhKbmJvPQ==";

        System.out.println("======:");
        System.out.println(encodeResult);
        String decodeResult = rsaDecode(encodeResult, priKey);

        System.out.println(decodeResult);

        String encTranData = "ekozbTlIbmpCUUFPSDdiaDVmcWpvZnZZNXFHZVZaeDdzN1hyelpnb0o1eE5PNFBrNURsMWlGSmdTTlpFWEwzYw==";

        String s =  new AES(Mode.CBC, Padding.PKCS5Padding, decodeResult.getBytes(), "0392039203920300".getBytes()).decryptStr(encTranData);

        System.out.println(s);

    }


}
